package com.example.client_group5;

import java.util.ArrayList;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;

import util.HttpClientFactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.widget.ProgressBar;

public class UserItemAdapter extends ArrayAdapter<User> {
	private ArrayList<User> users;
	private Activity activity;
	//public  boolean [] is_following_flags;
	public 	ImageManager imageManager;
	public static class ViewHolder{
		public TextView fullname;
		public TextView username;
		public ImageView image;
		public Button follow;
		public ProgressBar progress; //ADDED
	}
	public UserItemAdapter(Activity a, int textViewResourceId, ArrayList<User> users) {
		super(a, textViewResourceId, users);
		this.activity=a;
		this.users = users;
		//this.imageManager=new ImageManager(a.getApplicationContext());
		this.imageManager=ImageManager.getInstance(a.getApplicationContext());
		/*this.is_following_flags=new boolean[users.size()];	
		for(int i=0;i<users.size();i++)
		{
			this.is_following_flags[i]=users.get(i).is_following;
		}*/
		
		
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
	
		//Log.d("tag", "position:"+position+";size:"+is_following_flags.length);
		View v=convertView;
		ViewHolder holder;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.listfollowing, null);
			holder=new ViewHolder();
			v.setTag(holder);
		}
		else
			holder=(ViewHolder)v.getTag();
		LinearLayout linear=(LinearLayout)v.findViewById(R.id.linearId);
		FrameLayout frame=(FrameLayout)v.findViewById(R.id.frameId);
		linear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				User user = users.get(position);
				Intent i = new Intent(activity, ProfileActivity.class);
				Bundle b = new Bundle();
				String [] info={Integer.toString(user.id),user.user_fullname,
						user.user_name,user.avatar,"home"};
				b.putStringArray("user", info);
				i.putExtras(b);
				activity.startActivity(i);
//				activity.finish();
				
			}
		});
		final User user = users.get(position);
		if(user!=null)
		{
			holder.fullname=(TextView)v.findViewById(R.id.fullname);
			holder.username=(TextView)v.findViewById(R.id.mention);
			holder.image=(ImageView)v.findViewById(R.id.avatar);
			holder.follow=(Button)v.findViewById(R.id.btFollow);
			
			holder.progress=(ProgressBar)v.findViewById(R.id.progress_bar);
			if(holder.fullname!=null)
			{
				holder.fullname.setText(user.user_fullname);
			}
			if(holder.username!=null)
			{
				holder.username.setText("@"+user.user_name);
			}
			if(holder.image!=null)
			{
				imageManager.displayImage(user.avatar, activity,holder.image, holder.progress); 
				holder.image.setTag(user.avatar);
				holder.image.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						User user = users.get(position);
						Intent i = new Intent(activity, ProfileActivity.class);
						Bundle b = new Bundle();
						String [] info={Integer.toString(user.id),user.user_fullname,
								user.user_name,user.avatar,"home"};
						b.putStringArray("user", info);
						i.putExtras(b);
						activity.startActivity(i);
//						activity.finish();
					}
				});
			}
			if(holder.follow!=null)
			{
				if(user.is_following)
					holder.follow.setBackgroundResource(R.drawable.bt_follow);
				else
					holder.follow.setBackgroundResource(R.drawable.bt_unfollow);
				holder.follow.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					
					HttpClient client=HttpClientFactory.getThreadSafeClient();
					//HttpClient client=SignInActivity.client;
					
					ResponseHandler<String> responseHandler = 
					        new BasicResponseHandler();
					Log.d("request","user"+user.id);
					//if(v.getResources().))
					//v.getBackground().
					if(user.is_following)
					{
						String getUrl = Setting.ipServer + "api/user/un-following/user_id/"
								+user.id;
						HttpGet get=new HttpGet(getUrl);
						v.setBackgroundResource(R.drawable.bt_unfollow);
						try {
						    client.execute(get, responseHandler); 
						    Log.d("unfollow", client.execute(get, responseHandler));
						} catch(Exception ex) {
							  ex.printStackTrace();
						}
						user.is_following=false;
						
					}
					else
					{
						String followUrl = Setting.ipServer + "api/user/follow/user_id/"
								+user.id;
						HttpGet follow=new HttpGet(followUrl);
						v.setBackgroundResource(R.drawable.bt_follow);
						try {
						    client.execute(follow, responseHandler); 
						    Log.d("follow", client.execute(follow, responseHandler));
						} catch(Exception ex) {
							  ex.printStackTrace();
						}
						user.is_following=true;
					}
					}
				});
			}
		}
		return v;
	}
	
	
}
